package com.angel.web.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.core.util.TreeNodes;
import com.angel.dao.health.SysRoleMapper;
import com.angel.health.SysMenu;
import com.angel.health.SysMenuExt;
import com.angel.health.SysRole;
import com.angel.health.SysUser;
import com.angel.provider.sys.SysManagerMenuProvider;
import com.angel.provider.sys.SysManagerRoleProvider;

/**
 * 高亚林
 * 2016年9月20日下午1:06:15
 */

@RequestMapping("/manag/menu")
@Controller
public class SysManagerMenuController {

	@Autowired
	private SysManagerMenuProvider provider;
	@Autowired
	private SysRoleMapper mapper;
	
	//分页查询
	@RequestMapping("/index")
	public String index(SysMenu menu,HttpServletRequest request){
		List<Map<String, Object>> list = provider.findAll(menu);
		request.setAttribute("list", list);
		return "manag/menu";
	}
	
	//根据用户查询所有菜单
	@RequestMapping(value="findAllMenu", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String findAllMenu(HttpServletRequest request){
		SysUser user = (SysUser) request.getSession().getAttribute("currentUser");
		List<SysMenu> list = new ArrayList<SysMenu>();
		List<SysRole> roleList = mapper.findRolesByUser(user.getId());
		int i = 0;
		for (SysRole sysRole : roleList) {
			if(sysRole.getId() == 1){
				list = provider.findMenus(new SysMenu());
				i = 1;
			}
		}
		if(i == 0){
			list = provider.findAllMenu(user);
		}
		List<TreeNodes> treeList = new ArrayList<TreeNodes>();
		for (SysMenu sysMenu : list) {
			TreeNodes node = new TreeNodes();
			node.setId(sysMenu.getId());
			node.setpId(sysMenu.getParentId());
			node.setpIds(sysMenu.getParentIds());
			node.setName(sysMenu.getName());
			node.setFile(sysMenu.getIcon());
			node.setUrl(sysMenu.getHref());
			treeList.add(node);
		}
		return JsonUtil.toJson(treeList);
	}
	
	
	//根据所有菜单
	@RequestMapping(value="findMenus", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String findMenus(SysMenu menu){
		List<SysMenu> list = provider.findMenus(menu);
		List<TreeNodes> treeList = new ArrayList<TreeNodes>();
		for (SysMenu sysMenu : list) {
			TreeNodes node = new TreeNodes();
			node.setId(sysMenu.getId());
			node.setpId(sysMenu.getParentId());
			node.setpIds(sysMenu.getParentIds());
			node.setName(sysMenu.getName());
			node.setFile(sysMenu.getIcon());
			//node.setUrl(sysMenu.getHref());
			treeList.add(node);
		}
		return JsonUtil.toJson(treeList);
	}
	
	//删除单个用户
	@RequestMapping("deleteById")
	@ResponseBody
	public String deleteById(SysMenu menu){
		int i = provider.deleteById(menu);
		return JsonUtil.toJson(i);
	}
	
	//保存
	@RequestMapping("/save")
	@ResponseBody
	public String save(SysMenu menu,HttpServletRequest request){
		menu.setCreateBy(request.getSession().getAttribute("user").toString());
		Map<String, String> map = provider.save(menu);
		return JsonUtil.toJson(map);
	}
	
	//根据id获取数据
	@RequestMapping("/edit")
	@ResponseBody
	public String edit(SysMenu menu){
		SysMenuExt ext = provider.findById(menu);
		return JsonUtil.toJson(ext);
	}
	
	//修改
	@RequestMapping("/update")
	@ResponseBody
	public String update(SysMenu menu,HttpServletRequest request){
		menu.setUpdateBy(request.getSession().getAttribute("user").toString());
		Map<String, String> map = provider.update(menu);
		return JsonUtil.toJson(map);
	}
}
